package gfx;

import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;

import util.Logger;
import util.VertexBuffer;

public class Image {
	private float mGLWidth;
	private float mGLHeight;
	
	// Texture stuff
	private FloatBuffer mTextureBuffer;
	private int[] mTextures;

	private FloatBuffer mVertexBuffer;

	private float vertices[];

	public Image(short graphic, float glWidth, float glHeight){
		mGLWidth  = glWidth;
		mGLHeight = glHeight;
		mTextures = Spritesheet.getInstance().getTexture(graphic);
		calculateVertices();

		float texture[] = {
				0.0f, 1.0f,
				0.0f, 0.0f,
				1.0f, 1.0f,
				1.0f, 0.0f
		};
		
		mTextureBuffer = VertexBuffer.getVertexBuffer(texture);
	}
	
	public void calculateVertices(){
		vertices = new float[]{
				-mGLWidth/2,-mGLHeight/2,	0, // V2
				-mGLWidth/2, mGLHeight/2,	0, // V1
				 mGLWidth/2,-mGLHeight/2,	0, // V3
				 mGLWidth/2, mGLHeight/2,	0  // V4
		};
		mVertexBuffer = VertexBuffer.getVertexBuffer(vertices);
	}
	
	public void draw(GL10 gl, PointF loc){
		if(loc == null)
			draw(gl, 0, 0, 1, 1, 1, 1);
		else draw(gl, loc.x, loc.y, 1, 1, 1, 1);
	}
	
	public void draw(GL10 gl, float x, float y){
		draw(gl, x, y, 1, 1, 1, 1);
	}
			
	public void draw(GL10 gl, float x, float y, float r, float g, float b, float a){
		if(mTextures == null) return;
		gl.glPushMatrix();
		
		// Bind Texture
//		gl.glBindTexture(GL10.GL_TEXTURE_2D, Spritesheet.getInstance().tempTextureTest[0]);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]);

		// Point to Buffers
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

		// Set Face
		gl.glFrontFace(GL10.GL_CW);
		// Point to buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
		if(r != 1 || g != 1 || b != 1 || a != 1)
			gl.glColor4f(r, g, b, a);
		gl.glTranslatef(x, y, 0);	
		
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);//vertices.length / 3);

		// Disable Client State before Leaving
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glColor4f(1, 1, 1, 1);
		gl.glPopMatrix();
	}
	
	public void draw(GL10 gl, PointF loc, float rotation){
		draw(gl, loc.x, loc.y, 1, 1, 1, 1, rotation);
	}
	
	public void draw(GL10 gl, float x, float y, float rotation){
		draw(gl, x, y, 1, 1, 1, 1, rotation);
	}
			
	public void draw(GL10 gl, float x, float y, float r, float g, float b, float a, float rotation){
		if(mTextures == null) return;
		gl.glPushMatrix();
		
		// Bind Texture
//		gl.glBindTexture(GL10.GL_TEXTURE_2D, Spritesheet.getInstance().tempTextureTest[0]);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]);

		// Point to Buffers
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

		// Set Face
		gl.glFrontFace(GL10.GL_CW);
		// Point to buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
		if(r != 1 || g != 1 || b != 1 || a != 1)
			gl.glColor4f(r, g, b, a);
		gl.glTranslatef(x, y, 0);	
		
		gl.glRotatef(rotation, 0, 0, 1);
		
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);//vertices.length / 3);

		// Disable Client State before Leaving
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glColor4f(1, 1, 1, 1);
		gl.glPopMatrix();
	}
	

	
	public void draw(GL10 gl){
		if(mTextures == null) return;
		gl.glPushMatrix();
		
		// Bind Texture
//		gl.glBindTexture(GL10.GL_TEXTURE_2D, Spritesheet.getInstance().tempTextureTest[0]);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]);

		// Point to Buffers
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

		// Set Face
		gl.glFrontFace(GL10.GL_CW);
		// Point to buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
		
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);//vertices.length / 3);

		// Disable Client State before Leaving
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glColor4f(1, 1, 1, 1);
		gl.glPopMatrix();
	}

	public float getWidth() { return mGLWidth; }
	public float getHeight() { return mGLHeight; }
	public void setWidth(float width) { mGLWidth = width; calculateVertices(); }
	public void setHeight(float height) { mGLHeight = height; calculateVertices(); }
}
